package logic

import (
	"context"
	"database/sql"
	"goods/internal/model"
	"log"
	"time"

	"goods/goods"
	"goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateCategoryLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCategoryLogic {
	return &CreateCategoryLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 分类
func (l *CreateCategoryLogic) CreateCategory(in *goods.CreateCategoryRequest) (*goods.CreateCategoryResponse, error) {
	// todo: add your logic here and delete this line
	category := model.Category{
		Name:       in.Data.Name,
		Sort:       int64(in.Data.Sort),
		Status:     sql.NullInt64{Int64: int64(in.Data.Status), Valid: true},
		CreateTime: sql.NullTime{Time: time.Now(), Valid: false},
	}
	_, err := l.svcCtx.Category.Insert(context.Background(), &category)
	if err != nil {
		log.Println("Error inserting category")
	}
	return &goods.CreateCategoryResponse{Code: 200, Msg: "添加成功"}, nil
}
